﻿using AutomaticScrewLocking.domain.modbus.comm.model.entity;
using AutomaticScrewLocking.types;
using System;
using System.Windows.Forms;

namespace AutomaticScrewLocking.app
{
    public partial class ParamSetForm: Form
    {
        private CommParam commParam;
        public ParamSetForm()
        {
            commParam = GlobalParam.getInstance().param;

            InitializeComponent();
            LoadParam();
        }

        /// <summary>
        /// 加载参数
        /// </summary>
        private void LoadParam()
        {
            IpTxb.Text = commParam.plcIp;
            PortTxb.Text = commParam.plcPort.ToString();
            StartTxb.Text = commParam.startPos.ToString();
            EndTxb.Text = commParam.endPos.ToString();
            UpLimitTxb.Text = commParam.screwLimit.ToString();
        }

        private void SaveParm()
        {
            try
            {
                commParam.plcIp = IpTxb.Text;
                commParam.plcPort = Convert.ToInt32(PortTxb.Text);
                commParam.startPos = Convert.ToSingle(StartTxb.Text);
                commParam.endPos = Convert.ToSingle(EndTxb.Text);
                commParam.screwLimit = Convert.ToSingle(UpLimitTxb.Text);

                GlobalParam.getInstance().saveParam(out string errorMsg);
                MessageBox.Show("参数保存成功");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }

        private void SaveParamBtn_Click(object sender, EventArgs e)
        {
            SaveParm();
        }
    }
}
